

#include "equipmentdetaillayer.h"
#include "resourcemanager.h"
#include "log.h"
#include "messagequeue.h"
#include "gamemanager.h"
#include "gamedatamanager.h"
#include "cocos-ext.h"

int EquipmentDetailLayer::attack_column1_x = 130;
int EquipmentDetailLayer::attack_column2_x = 195;
int EquipmentDetailLayer::resist_column1_x = 275;
int EquipmentDetailLayer::resist_column2_x = 340;
int EquipmentDetailLayer::first_row_y = 158;
int EquipmentDetailLayer::row_height = 32;
int EquipmentDetailLayer::label_value_space = 40;

EquipmentDetailLayer::EquipmentDetailLayer() {
}

EquipmentDetailLayer::~EquipmentDetailLayer() {
}

bool EquipmentDetailLayer::init() {
	if (!CCLayer::init()) {
		return false;
	}

	setTouchEnabled(false);

	setContentSize(CCSize(410, 410));
	setScale(GM->minscale);

	auto sceneBg = cocos2d::extension::CCScale9Sprite::create("textures/pad_bg.png", CCRectMake(0, 0, 225, 225), CCRectMake(100, 100, 25, 25));
	sceneBg->setContentSize(CCSize(410, 410));
	sceneBg->setPosition(ccp(205, 205));
	sceneBg->setAnchorPoint(ccp(0.5f, 0.5f));
	addChild(sceneBg);

	_btnEdit = RM->makeButton("button/blue_button_0.png", "button/blue_button_0.png", nullptr, this, menu_selector(EquipmentDetailLayer::onEdit), this);
	_btnEdit->setPosition(ccp(65, 35));
	RM->makeButtonLabel("Edit", 24, _btnEdit);

	_labelTitle = CCLabelTTF::create("", "", 32);
	_labelTitle->setHorizontalAlignment(kCCTextAlignmentCenter);
	_labelTitle->setPosition(ccp(205, 356));
	addChild(_labelTitle);

	_labelDesc = CCLabelTTF::create("", "", 14);
	_labelDesc->setAnchorPoint(ccp(0.0f, 1.0f));
	_labelDesc->setPosition(ccp(125, 333));
	addChild(_labelDesc);

	_labelRarity = CCLabelTTF::create("Common", "", 18);
	_labelRarity->setHorizontalAlignment(kCCTextAlignmentCenter);
	_labelRarity->setPosition(ccp(76, 324));
	addChild(_labelRarity);

	_labelPoints = CCLabelTTF::create("0", "", 36);
	_labelPoints->setHorizontalAlignment(kCCTextAlignmentCenter);
	_labelPoints->setPosition(ccp(76, 280));
	addChild(_labelPoints);

	CCLabelTTF *labelDefenseTitle = CCLabelTTF::create("Defense", "", 18);
	labelDefenseTitle->setHorizontalAlignment(kCCTextAlignmentCenter);
	labelDefenseTitle->setPosition(ccp(65, 203));
	addChild(labelDefenseTitle);

	_labelDefense = CCLabelTTF::create("0", "", 18);
	_labelDefense->setHorizontalAlignment(kCCTextAlignmentCenter);
	_labelDefense->setPosition(ccp(65, 180));
	addChild(_labelDefense);

	CCLabelTTF *labelRangeTitle = CCLabelTTF::create("Range", "", 18);
	labelRangeTitle->setHorizontalAlignment(kCCTextAlignmentCenter);
	labelRangeTitle->setPosition(ccp(65, 154));
	addChild(labelRangeTitle);

	_labelRange = CCLabelTTF::create("0", "", 18);
	_labelRange->setHorizontalAlignment(kCCTextAlignmentCenter);
	_labelRange->setPosition(ccp(65, 130));
	addChild(_labelRange);

	_labelCastingTimeTitle = CCLabelTTF::create("CastingTime", "", 18);
	_labelCastingTimeTitle->setHorizontalAlignment(kCCTextAlignmentCenter);
	_labelCastingTimeTitle->setPosition(ccp(65, 100));
	addChild(_labelCastingTimeTitle);

	_labelReloadTimeTitle = CCLabelTTF::create("ReloadTime", "", 18);
	_labelReloadTimeTitle->setHorizontalAlignment(kCCTextAlignmentCenter);
	_labelReloadTimeTitle->setPosition(ccp(65, 100));
	addChild(_labelReloadTimeTitle);

	_labelTime = CCLabelTTF::create("0", "", 18);
	_labelTime->setHorizontalAlignment(kCCTextAlignmentCenter);
	_labelTime->setPosition(ccp(65, 76));
	addChild(_labelTime);

	int count = (int) DamageType::count;
	int y = first_row_y;

	for (int i = 0; i < count; i++) {
		sprintf(buf, "icon/%s.png", DAMAGE_TYPE_NAMES[i]);
		CCSprite *spr = MAKE_SPRITE(buf);
		spr->setScale(1.0f);
		spr->setPosition(ccp((i % 2 == 0) ? attack_column1_x : attack_column2_x, y));
		addChild(spr);

		_attackValueLabels[i] = spr;

		CCLabelTTF *label = CCLabelTTF::create("0", "", 16);
		label->setPosition(ccp((i % 2 == 0) ? attack_column1_x + label_value_space : attack_column2_x + label_value_space, y));
		addChild(label);
		_attackValues[i] = label;

		sprintf(buf, "icon/%s.png", DAMAGE_TYPE_NAMES[i]);
		spr = MAKE_SPRITE(buf);
		spr->setScale(1.0f);
		spr->setPosition(ccp((i % 2 == 0) ? resist_column1_x : resist_column2_x, y));
		addChild(spr);

		label = CCLabelTTF::create("0", "", 16);
		label->setPosition(ccp((i % 2 == 0) ? resist_column1_x + label_value_space : resist_column2_x + label_value_space, y));
		addChild(label);
		_resistValues[i] = label;

		_resistValueLabels[i] = spr;

		if (i % 2 == 1) {
			y -= row_height;
		}
	}

	return true;
}

void EquipmentDetailLayer::onClose(CCObject *sender) {
	CCDirector::sharedDirector()->popScene();
}

void EquipmentDetailLayer::setAttribute(const string& name, const EquipmentAttribute& attr, bool allowEdit) {
	char buf[100];

	static const ccColor3B rarity_colors[] = {
			ccc3(0, 0, 0),
			ccc3(35, 142, 23),
			ccc3(23, 95, 170),
			ccc3(197, 116, 20),
	};

	_labelTitle->setString(name.c_str());
	_labelRarity->setString(RARITY_NAMES[(int) attr.rarity]);

	_labelTitle->setColor(rarity_colors[(int)attr.rarity]);
	_labelRarity->setColor(rarity_colors[(int)attr.rarity]);

	_labelDesc->setString(attr.desc.c_str());
	sprintf(buf, "%d", attr.points);
	_labelPoints->setString(buf);
	sprintf(buf, "%d", attr.defense);
	_labelDefense->setString(buf);
	sprintf(buf, "%d", attr.range);
	_labelRange->setString(buf);
	if (attr.castingTime > 0) {
		sprintf(buf, "%d s", attr.castingTime);
		_labelTime->setString(buf);
	}
	if (attr.reloadTime > 0) {
		sprintf(buf, "%d s", attr.reloadTime);
		_labelTime->setString(buf);
	}

	_labelCastingTimeTitle->setVisible(attr.castingTime > 0);
	_labelReloadTimeTitle->setVisible(attr.reloadTime > 0);

	_labelTime->setVisible(attr.castingTime > 0 || attr.reloadTime > 0);

	int arrAttacks[] = {
			attr.slashAttack, // slash
			attr.bluntAttack, // blunt
			attr.piercingAttack, // piercing
			attr.explosionAttack, // explosion
			attr.fireAttack, // fire
			attr.iceAttack, // ice
			attr.poisonAttack, // poison
			attr.thunderAttack, // thunder
			attr.darkAttack, // dark
			attr.holyAttack, // holy
	};

	int y = first_row_y;
	int n = 0;

	for(int i = 0; i < (int)DamageType::count; ++i) {
		sprintf(buf, "%d", arrAttacks[i]);
		_attackValues[i]->setString(buf);

		_attackValueLabels[i]->setVisible(arrAttacks[i] > 0);
		_attackValues[i]->setVisible(arrAttacks[i] > 0);

		if(arrAttacks[i] > 0) {
			int x = (n % 2 == 0 ? attack_column1_x : attack_column2_x);
			_attackValueLabels[i]->setPosition(ccp(x, y));
			_attackValues[i]->setPosition(ccp(x + label_value_space, y));

			if(n % 2 == 1) {
				y -= row_height;
			}

			++n;
		}
	}

	int arrResists[] = {
			attr.slashResist, // slash
			attr.bluntResist, // blunt
			attr.piercingResist, // piercing
			attr.explosionResist, // explosion
			attr.fireResist, // fire
			attr.iceResist, // ice
			attr.poisonResist, // poison
			attr.thunderResist, // thunder
			attr.darkResist, // dark
			attr.holyResist, // holy
	};

	y = first_row_y;
	n = 0;

	for(int i = 0; i < (int)DamageType::count; ++i) {
		sprintf(buf, "%d", arrResists[i]);
		_resistValues[i]->setString(buf);

		_resistValueLabels[i]->setVisible(arrResists[i] > 0);
		_resistValues[i]->setVisible(arrResists[i] > 0);

		if(arrResists[i] > 0) {
			int x = (n % 2 == 0 ? resist_column1_x : resist_column2_x);
			_resistValueLabels[i]->setPosition(ccp(x, y));
			_resistValues[i]->setPosition(ccp(x + label_value_space, y));

			if(n % 2 == 1) {
				y -= row_height;
			}

			++n;
		}
	}

	_btnEdit->setVisible(allowEdit);
}

void EquipmentDetailLayer::onEdit(CCObject *sender) {

}
